package com.code.demo.util;

import android.annotation.SuppressLint;
import android.content.Context;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by A on 2016/4/15.
 */
public class DateUtil {
    public static final SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat sdformat1 = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat sdformat2 = new SimpleDateFormat("yyyy-MM");
    public static final SimpleDateFormat sdformat3 = new SimpleDateFormat("yyyy年M月d日 HH:mm");
    public static final SimpleDateFormat sdformat4 = new SimpleDateFormat("yyyyMMdd");

    /**
     * 模式11
     */
    public static final SimpleDateFormat mDateFormat10 = new SimpleDateFormat(
            "yyyy年MM月dd日");

    public static final SimpleDateFormat mDateFormat11 = new SimpleDateFormat(
            "yyyy-MM-dd");

    public static final SimpleDateFormat mDateFormat12 = new SimpleDateFormat(
            "yyyy年MM月dd日 HH:mm:ss");

    public static final SimpleDateFormat mDateFormat13 = new SimpleDateFormat(
            "yyyy年MM月dd日HH:mm:ss");

    public static final SimpleDateFormat mDateFormat14 = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    public static final SimpleDateFormat mDateFormat15 = new SimpleDateFormat(
            "yyyy年M月d日 HH:mm:ss");
    /**
     * 秒
     */
    public static final int SECOND_MILLIS = 1000;

    /**
     * 30秒
     */
    public static final int Half_MINUTE_MILLIS = 30 * SECOND_MILLIS;

    /**
     * 分
     */
    public static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;

    /**
     * 小时
     */
    private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;

    /**
     * 天
     */
    private static final int DAY_MILLIS = 24 * HOUR_MILLIS;

    //得出两个时间的差值，单位秒
    public static long TimeInMillis(String time1, String time2) {

        Date dateTime1 = null;
        Date dateTime2 = null;
        try {
            dateTime1 = sdformat.parse(time1);
            dateTime2 = sdformat.parse(time2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        GregorianCalendar cal1 = new GregorianCalendar();
        GregorianCalendar cal2 = new GregorianCalendar();
        cal1.setTime(dateTime1);
        cal2.setTime(dateTime2);
        long dayCount = (cal2.getTimeInMillis() - cal1.getTimeInMillis()) / 1000;
        return dayCount;
    }

    //显示距今多久
    public static String getTimeAgoNow(long time, Context ctx) {
        // 将时间单位转化为毫秒
        if (time < 1000000000000L) {
            // if timestamp given in seconds, convert to millis
            time *= 1000;
        }

        long now = getCurrentTime(ctx);
        if (time > now || time <= 0) {
            return "刚刚";
        }

        // TODO: localize
        final long diff = now - time;
        if (diff < 1 * MINUTE_MILLIS) {
            return "刚刚";
        } else if (diff < 60 * MINUTE_MILLIS) {
            return diff / MINUTE_MILLIS + "分钟前";
        } else if (diff < 24 * HOUR_MILLIS) {
            return diff / HOUR_MILLIS + "小时前";
//        } else if (diff < 10 * 24 * HOUR_MILLIS) {
//            return diff / DAY_MILLIS + "天前";
        }
        /**
         * 十天后
         */
        else {
            return mDateFormat11.format(new Date(time)).substring(0, 10);
        }
    }

    public static int getBeforeDay(long time, Context ctx) {
        // 将时间单位转化为毫秒
        if (time < 1000000000000L) {
            // if timestamp given in seconds, convert to millis
            time *= 1000;
        }

        String d = sdformat4.format(new Date(time));
        String n = sdformat4.format(new Date());

        Long t1 = Long.valueOf(d);
        Long t2 = Long.valueOf(n);

        if (t2-t1 == 0) {
            return 1;
        } else if (t2-t1 == 1) {
            return 2;
        }

        return 0;
    }

    public static String getDayString(long time) {
        // 将时间单位转化为毫秒
        if (time < 1000000000000L) {
            // if timestamp given in seconds, convert to millis
            time *= 1000;
        }

        String ret = mDateFormat15.format(new Date(time));
        int e = ret.indexOf('日');
        return ret.substring(6, e);
    }

    /**
     * 获取当前时间
     *
     * @param context
     * @return
     */
    public static long getCurrentTime(final Context context) {
        return System.currentTimeMillis();
    }

    public static String[] computingTime(String minTime, String maxTime) {
        String[] minTimes = minTime.split(":");
        String[] maxTimes = maxTime.split(":");
        int minTimeInt = Integer.parseInt(minTimes[0]) * 60 + Integer.parseInt(minTimes[1]);
        int maxTimeInt = Integer.parseInt(maxTimes[0]) * 60 + Integer.parseInt(maxTimes[1]);
        int diff = (maxTimeInt - minTimeInt) / 6;
        String[] caleValue = new String[8];
        int t;
        int hour;
        int minute;
        for (int i = 0; i < 8; i++) {
            t = minTimeInt + i * diff;
            hour = t / 60;
            minute = t % 60;
            if (minute == 0) {
                caleValue[i] = hour + ":" + minute + "0";
            } else {
                caleValue[i] = hour + ":" + minute;
            }

        }
        return caleValue;
    }

    public static String[] weekName = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    public static String[] weekDayName = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    public static int getMonthDays(int year, int month) {
        if (month > 12) {
            month = 1;
            year += 1;
        } else if (month < 1) {
            month = 12;
            year -= 1;
        }
        int[] arr = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int days = 0;

        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            arr[1] = 29; // 闰年2月29天
        }

        try {
            days = arr[month - 1];
        } catch (Exception e) {
            e.getStackTrace();
        }

        return days;
    }

    public static int getYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    public static int getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }

    public static int getCurrentMonthDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    public static int getWeekDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
    }

    public static String getWeekDayName() {
        return weekDayName[getWeekDay() - 1];
    }

    public static int getHour() {
        return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    }

    public static int getMinute() {
        return Calendar.getInstance().get(Calendar.MINUTE);
    }

    public static int[] getWeekSunday(int year, int month, int day, int pervious) {
        int[] time = new int[3];
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.add(Calendar.DAY_OF_MONTH, pervious);
        time[0] = c.get(Calendar.YEAR);
        time[1] = c.get(Calendar.MONTH) + 1;
        time[2] = c.get(Calendar.DAY_OF_MONTH);
        return time;

    }

    public static int getWeekDayFromDate(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDateFromString(year, month));
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return week_index;
    }

    @SuppressLint("SimpleDateFormat")
    public static Date getDateFromString(int year, int month) {
        String dateString = year + "-" + (month > 9 ? month : ("0" + month))
                + "-01";
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        return date;
    }

    /**
     * 时间戳
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static long getTime(String time) throws ParseException {
        Date date = sdformat.parse(time);
        long timeStemp = date.getTime();
        return timeStemp;
    }

}
